package cn.fly.springbucks.controller;

import cn.fly.springbucks.data.mybatis.model.Coffee;
import cn.fly.springbucks.service.CoffeeService;
import cn.fly.springbucks.support.ResponseMsg;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/coffee")
public class CoffeeController {

    @Autowired
    CoffeeService coffeeService;

    @RequestMapping(path = "/",params = "!name")
    public List<Coffee> getAllPage(@RequestParam int pageNum){
        return coffeeService.getAllCoffeeMenu(pageNum,1);
    }

    @RequestMapping(path = "/",params = "name")
    public Coffee getCoffeeByName(@RequestParam String name){
        return coffeeService.getCoffeeByName(name);
    }

    @RequestMapping("/addcoffee")
    public ResponseMsg insertCoffee(@RequestParam String name,@RequestParam Money price){
        return ResponseMsg.success(coffeeService.insertCoffee(Coffee.builder()
                                         .name(name)
                                         .price(price)
                                         .build()));
    }

    @RequestMapping(path = "/xml/",params = "!name",produces = MediaType.APPLICATION_XML_VALUE)
    public List<Coffee> getAllPageXML(@RequestParam int pageNum){
        return coffeeService.getAllCoffeeMenu(pageNum,1);
    }

    @RequestMapping(path = "/xml/",params = "name")
    public Coffee getCoffeeByNameXML(@RequestParam String name){
        return coffeeService.getCoffeeByName(name);
    }

    @RequestMapping(path = "/xml/addcoffee")
    public ResponseMsg insertCoffeeXML(@RequestParam String name,@RequestParam Money price){
        return ResponseMsg.success(coffeeService.insertCoffee(Coffee.builder()
                .name(name)
                .price(price)
                .build()));
    }
}
